package cc.mrbird.febs.common.handler;

import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.utils.FebsUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FebsAuthExceptionEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
						 AuthenticationException authException) throws IOException {
		FebsResponse febsResponse = new FebsResponse();
		FebsUtil.makeResponse(response,
				MediaType.APPLICATION_JSON_UTF8_VALUE,
				HttpServletResponse.SC_UNAUTHORIZED,
				JSONObject.toJSONString(febsResponse.message("token无效")).getBytes());
	}
}